package pais.db;

import java.io.IOException;
import java.util.Date;

import papos.rowset.AdvRandomAccessFile;
import papos.rowset.KeyDesc;
import papos.rowset.Storable;


public class HtmlPage implements Storable {
  public static final long NULL_UID = 0;
  public static final long NULL_CHANGE_DATE = 0;
  public static final long INVALUD_URL_CHANGE_DATE = -1;
  public static final long HTML_PARSER_ERROR_CHANGE_DATE = -2;
  public static final int MAX_URL_LEN = 256;
  public static final int MAX_TITLE_LEN = 80;

  /* fields should be public in order to be stored in StorageSystem */
  public long UID_; // primary key
  public String URL_;
  public String title_;
  public long changeDate_;
  public int connectFailCount_;

  public HtmlPage() {
  }

  public HtmlPage(long UID, String URL, String title, long changeDate,int connectFailCount) {
    UID_ = UID;
    setURL(URL);
    setTitle(title);
    changeDate_ = changeDate;
    connectFailCount_ = connectFailCount;
  }

  public KeyDesc[] getKeyDesc() {
    return new KeyDesc[] { 
        new KeyDesc("UID_", false),
        new KeyDesc("URL_", false),
        new KeyDesc("changeDate_", true) };
  }

  public int maxSize() {
    return 8 + (4 + MAX_URL_LEN) + (4 + MAX_TITLE_LEN) + 8 + 4;
  }

  public Object clone() {
    return new HtmlPage(UID_, URL_, title_, changeDate_, connectFailCount_);
  }

  public void writeObject(AdvRandomAccessFile raf) throws IOException {
    raf.writeLong(UID_);
    raf.writeString(URL_,MAX_URL_LEN);
    raf.writeString(title_,MAX_TITLE_LEN);
    raf.writeLong(changeDate_);
    raf.writeInt(connectFailCount_);
  }

  public void readObject(AdvRandomAccessFile raf) throws IOException,
  ClassNotFoundException {
    UID_ = raf.readLong();
    URL_ = raf.readString(MAX_URL_LEN);
    title_ = raf.readString(MAX_TITLE_LEN);
    changeDate_ = raf.readLong();
    connectFailCount_ = raf.readInt();
  }

  public String getURL() {
    return URL_;
  }

  public void setURL(String url) {
    URL_ = url;
  }

  public long getUID() {
    return UID_;
  }

  public void setUID(long uid) {
    UID_ = uid;
  }

  public long getChangeDate() {
    return changeDate_;
  }

  public void setChangeDate(long changeDate) {
    this.changeDate_ = changeDate;
  }

  public boolean isIndexed() {
    return changeDate_ != NULL_CHANGE_DATE;
  }

  public int getConnectFailCount() {
    return connectFailCount_;
  }

  public void setConnectFailCount(int connectFailCount) {
    this.connectFailCount_ = connectFailCount;
  }

  public String getTitle() {
    return title_;
  }

  public void setTitle(String title) {
    this.title_ = title;
  }

  public String toString() {
    StringBuffer sb = new StringBuffer();
    sb.append("A URL Summary");
    sb.append("uid = ");
    sb.append(getUID());
    sb.append("\ntitle = ");
    sb.append(getTitle());
    sb.append("\nurl = ");
    sb.append(getURL());
    sb.append("\nstatus = ");

    long changedDate = getChangeDate();
    if ( changedDate == 0 ) {
      sb.append("not indexed yet");
    }
    if ( changedDate == -1) {
      sb.append("invalid url");
    }
    if ( changedDate == -2) {
      sb.append("html parse error");
    }
    if ( changedDate > 0 ) {
      sb.append("indexed");
    }

    sb.append("\nconnect fail count = " + getConnectFailCount() + "\n");

    if (getChangeDate() > 0) {
      sb.append("\ndocument changed date = " + new Date(getChangeDate())+"\n");
    }
    return sb.toString();
  }
}
